package org.jeecg.modules.demo.zyExam.zyexamaddpaper.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.zyExam.zyPaperMultiQuestion.entity.ZyPaperMultiQuestion;
import org.jeecg.modules.demo.zyExam.zyPaperMultiQuestion.service.impl.ZyPaperMultiQuestionServiceImpl;
import org.jeecg.modules.demo.zyExam.zyPaperMultipleChoiceQuestion.entity.ZyPaperMultipleChoiceQuestion;
import org.jeecg.modules.demo.zyExam.zyPaperMultipleChoiceQuestion.service.impl.ZyPaperMultipleChoiceQuestionServiceImpl;
import org.jeecg.modules.demo.zyExam.zyexamaddpaper.entity.ZyExamAddPaper;
import org.jeecg.modules.demo.zyExam.zyexamaddpaper.service.IZyExamAddPaperService;
import org.jeecg.modules.demo.zyExam.zyexamaddpaper.vo.QuestionNumber;
import org.jeecg.modules.demo.zyExam.zypaperfillquestion.entity.ZyPaperFillQuestion;
import org.jeecg.modules.demo.zyExam.zypaperfillquestion.service.impl.ZyPaperFillQuestionServiceImpl;
import org.jeecg.modules.demo.zyExam.zypaperjudgequestion.entity.ZyPaperJudgeQuestion;
import org.jeecg.modules.demo.zyExam.zypaperjudgequestion.service.impl.ZyPaperJudgeQuestionServiceImpl;
import org.jeecg.modules.demo.zyExam.zypapermanage.service.impl.ZyPaperManageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述
 *
 * @program: jeecg-boot-parent
 * @description:
 * @author: wwq
 * @create: 2024-01-08 08:49
 **/
@Tag(name = "查阅试卷")
@RestController
@RequestMapping("/zypapershow/zyPaperShow")
@Slf4j
public class zyPaperDetailShowController extends JeecgController<ZyExamAddPaper, IZyExamAddPaperService> {
    @Autowired
    private IZyExamAddPaperService zyExamAddPaperService;
    @Autowired
    private ZyPaperFillQuestionServiceImpl zyPaperFillQuestionService;

    @Autowired
    private ZyPaperJudgeQuestionServiceImpl zyPaperJudgeQuestionService;
    @Autowired
    private ZyPaperMultiQuestionServiceImpl zyPaperMultiQuestionService;

    @Autowired
    private ZyPaperMultipleChoiceQuestionServiceImpl zyPaperMultipleChoiceQuestionService;
    @Autowired
    private ZyPaperManageServiceImpl zyPaperManageService;



    @PostMapping("/autoMixQuestion")
    @Operation(summary = "auto_mix_question-根据题目类型的题目数量，自动组卷试卷题目", description = "auto_mix_question-根据题目类型的题目数量，自动组卷试卷题目")
    public Result ItemController(@RequestBody QuestionNumber questionNumber) {
        Integer changeNumber = questionNumber.getChangeNumber();
        Integer judgeNumber = questionNumber.getJudgeNumber();
        Integer fillNumber = questionNumber.getFillNumber();
        String paperCode = questionNumber.getPaperId();
//        questionNumber.setEnthic("JOB_TYPE_240");
        List<String> changeNumbers= zyPaperMultiQuestionService.findBySubject(questionNumber.getSubject(),changeNumber);
        List<String> multiChoiceNumbers=zyPaperMultipleChoiceQuestionService.findBySubject(questionNumber.getSubject(),changeNumber);
        List<String> judgeNumbers=zyPaperJudgeQuestionService.findBySubject(questionNumber.getSubject(),judgeNumber);
        List<String> fillNumbers=zyPaperFillQuestionService.findBySubject(questionNumber.getSubject(),fillNumber);
        System.out.println("changeNumbers:"+changeNumbers);
        System.out.println("multiChoiceNumbers:"+multiChoiceNumbers);
        System.out.println("judgeNumbers:"+judgeNumbers);
        System.out.println("fillNumbers:"+fillNumbers);
        if(changeNumbers == null || changeNumbers.size() != changeNumber){
            return Result.error("科目【" + questionNumber.getSubject() + "】题库【选择题】题目数量不足【" + changeNumber + "】，组卷失败",null);
        }
        if(multiChoiceNumbers == null || multiChoiceNumbers.size() != changeNumber){
            return Result.error("科目【" + questionNumber.getSubject() + "】题库【多选题】题目数量不足【" + changeNumber + "】，组卷失败",null);
        }
        if(fillNumbers == null || fillNumbers.size() != fillNumber) {
            return Result.error("科目【" + questionNumber.getSubject() + "】题库【填空题】题目数量不足【" + fillNumber + "】，组卷失败",null);
        }
        if(judgeNumbers == null || judgeNumbers.size() != judgeNumber){
            return Result.error("科目【" + questionNumber.getSubject() + "】题库【判断题】题目数量不足【" + judgeNumber + "】，组卷失败",null);
        }
        // 符合组题条件，执行组题
        // 选择题
        for (String number : changeNumbers) {
            org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage paperManage = new org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage(paperCode,1,number);
            boolean index = zyPaperManageService.save(paperManage);
            if(!index)
                return Result.error("选择题组卷保存失败");
        }
        // 多选题
        for (String multiChoiceNum : multiChoiceNumbers) {
            org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage paperManage = new org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage(paperCode,0,multiChoiceNum);
            boolean index = zyPaperManageService.save(paperManage);
            if(!index)
                return Result.error("多选题组卷保存失败");
        }
        // 填空题
        for (String fillNum : fillNumbers) {
            org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage paperManage = new org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage(paperCode,2,fillNum);
            boolean index = zyPaperManageService.save(paperManage);
            if(!index)
                return Result.error("填空题题组卷保存失败");
        }
        // 判断题
        for (String judge : judgeNumbers) {
            org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage paperManage = new org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage(paperCode,3,judge);
            boolean index = zyPaperManageService.save(paperManage);
            if(!index)
                return Result.error("判断题题组卷保存失败");
        }

        return Result.ok("试卷组卷成功");
    }
    @PostMapping("/autoMixQuestionTest")
    @Operation(summary = "auto_mix_question-根据题目类型的题目数量，自动组卷试卷题目", description = "auto_mix_question-根据题目类型的题目数量，自动组卷试卷题目")
    public Result ItemControllerTest(@RequestBody ZyExamAddPaper questionNumber) {
        Integer changeNumber = questionNumber.getMutilQuestionNumber();
        Integer judgeNumber = questionNumber.getJudgeQuestionNumber();
        Integer fillNumber = questionNumber.getFillQuestionNumber();
        String paperCode = questionNumber.getExamCode();
        String siteId = questionNumber.getSiteId();
//        questionNumber.setEnthic("JOB_TYPE_240");
        List<String> changeNumbers= zyPaperMultiQuestionService.findBySubject(questionNumber.getSource(),changeNumber);
        List<String> judgeNumbers=zyPaperJudgeQuestionService.findBySubject(questionNumber.getSource(),judgeNumber);
        List<String> fillNumbers=zyPaperFillQuestionService.findBySubject(questionNumber.getSource(),fillNumber);
        System.out.println("changeNumbers:"+changeNumbers);
        System.out.println("judgeNumbers:"+judgeNumbers);
        System.out.println("fillNumbers:"+fillNumbers);
        if(changeNumbers == null || changeNumbers.size() != changeNumber){
            return Result.error("科目【" + questionNumber.getSource() + "】题库【选择题】题目数量不足【" + changeNumber + "】，组卷失败",null);
        }
        if(fillNumbers == null || fillNumbers.size() != fillNumber) {
            return Result.error("科目【" + questionNumber.getSource() + "】题库【填空题】题目数量不足【" + fillNumber + "】，组卷失败",null);
        }
        if(judgeNumbers == null || judgeNumbers.size() != judgeNumber){
            return Result.error("科目【" + questionNumber.getSource() + "】题库【判断题】题目数量不足【" + judgeNumber + "】，组卷失败",null);
        }
        // 符合组题条件，执行组题
        // 选择题
        for (String number : changeNumbers) {
            org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage paperManage = new org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage(paperCode,1,number);
            boolean index = zyPaperManageService.save(paperManage);
            if(!index)
                return Result.error("选择题组卷保存失败",null);
        }

        // 填空题
        for (String fillNum : fillNumbers) {
            org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage paperManage = new org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage(paperCode,2,fillNum);
            boolean index = zyPaperManageService.save(paperManage);
            if(!index)
                return Result.error("填空题题组卷保存失败",null);
        }
        // 判断题
        for (String judge : judgeNumbers) {
            org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage paperManage = new org.jeecg.modules.demo.zyExam.zypapermanage.entity.ZyPaperManage(paperCode,3,judge);
            boolean index = zyPaperManageService.save(paperManage);
            if(!index)
                return Result.error("判断题题组卷保存失败",null);
        }
        zyExamAddPaperService.save(questionNumber);
        return Result.ok("试卷组卷成功");
    }
    @PostMapping("/autoMixQuestions")
    @Operation(summary = "auto_mix_question-根据题目类型的题目数量，自动组卷试卷题目", description = "auto_mix_question-根据题目类型的题目数量，自动组卷试卷题目")
    public Result ItemControllers(@RequestBody ZyExamAddPaper questionNumber) {
        Integer changeNumber = questionNumber.getMutilQuestionNumber();

        Integer judgeNumber = questionNumber.getJudgeQuestionNumber();
        Integer fillNumber = questionNumber.getFillQuestionNumber();
        String paperCode = questionNumber.getExamCode();
        LambdaQueryWrapper<ZyPaperMultiQuestion> lqw=new LambdaQueryWrapper<ZyPaperMultiQuestion>();
//        @Select("select id from zy_paper_multi_question  where subject =#{subject} order by rand() desc limit #{changeNumber}")
        lqw.eq(ZyPaperMultiQuestion::getSubject, questionNumber.getSource())
                .orderBy(true,true,ZyPaperMultiQuestion::getId)
                .last("limit " + changeNumber)
                .select(ZyPaperMultiQuestion::getId);
        return Result.ok();

    }

    @GetMapping(value = "/queryByPaperCode")
    @Operation(summary = "queryByPaperCode-根据试卷id查询题目", description = "queryByPaperCode-根据试卷id查询题目")
    public Result<Map<Integer, List<?>>> findByIdAndType(@RequestParam(required = false) String paperCode) {
        List<ZyPaperMultiQuestion> multiQuestionRes = zyPaperMultiQuestionService.findByIdAndType(paperCode);   //选择题题库 1
        List<ZyPaperMultipleChoiceQuestion> multipleChoiceQuestionsRes = zyPaperMultipleChoiceQuestionService.findByIdAndType(paperCode);
        List<ZyPaperFillQuestion> fillQuestionsRes = zyPaperFillQuestionService.findByIdAndType(paperCode);     //填空题题库 2
        List<ZyPaperJudgeQuestion> judgeQuestionRes = zyPaperJudgeQuestionService.findByIdAndType(paperCode);   //判断题题库 3
        Map<Integer, List<?>> map = new HashMap<>();
        map.put(1,multiQuestionRes);
        map.put(2,multipleChoiceQuestionsRes);
        map.put(3,fillQuestionsRes);
        map.put(4,judgeQuestionRes);
        System.out.println(map);
        return  Result.ok(map);
    }


    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZyExamAddPaper zyExamAddPaper) {

        return super.exportXls(request, zyExamAddPaper, ZyExamAddPaper.class, "出卷");
    }



}
